package com.learning.service.impl;

import com.learning.constant.MessageConstant;
import com.learning.context.BaseContext;
import com.learning.dto.ExamPaperQuestionDTO;
import com.learning.entity.ExamPaper;
import com.learning.entity.ExamPaperQuestion;
import com.learning.entity.Question;
import com.learning.enums.QuestionType;
import com.learning.exceptiom.PermissionErrorException;
import com.learning.exceptiom.QuestionRepeatError;
import com.learning.exceptiom.ScoreException;
import com.learning.mapper.ExamPaperMapper;
import com.learning.mapper.ExamPaperQuestionMapper;
import com.learning.mapper.QuestionMapper;
import com.learning.result.Result;
import com.learning.service.IExamPaperQuestionService;
import com.learning.service.IPermissionService;
import com.learning.vo.ExamPaperQuestionVO;
import com.learning.vo.PermissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * 试卷中的题目 服务实现类
 *
 * @author mhdtkc
 * @since 2024-5-29
 *
 */
@Service
public class ExamPaperQuestionServiceImpl implements IExamPaperQuestionService {

    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamPaperQuestionMapper examPaperQuestionMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private IPermissionService permissionService;

    /**
     * 添加题目到试卷 - 手动组卷
     * @param examPaperQuestionDTO
     */
    @Transactional
    @Override
    public void addQuestion(ExamPaperQuestionDTO examPaperQuestionDTO) {

        // 1.判断当前教师是否有组卷权限
        this.isSetAuth();

        ExamPaperQuestion examPaperQuestion = new ExamPaperQuestion();
        // 2.题目分数
        float questionScore = examPaperQuestionDTO.getQuestionScore();
        if(questionScore < 1f){
            //题目分数小于1，拋出分数异常
            throw new ScoreException(MessageConstant.QUESTION_SCORE_ERROR);
        }
        examPaperQuestion.setQuestionScore(questionScore);

        // 3.试卷id
        Long examPaperId = examPaperQuestionDTO.getExamPaperId();
        examPaperQuestion.setExamPaperId(examPaperId);

        // 4.题目id
        Long questionId = examPaperQuestionDTO.getQuestionId();
        // 判断题目是否在试卷中
        ExamPaperQuestion questionTest = examPaperQuestionMapper.queryByExamPaperIdAndQuestionId(examPaperId, questionId);
        if (questionTest != null){
            // 题目已在试卷中，抛出题目重复异常
            throw new QuestionRepeatError(MessageConstant.QUESTION_REPEAT_ERROR);
        }
        examPaperQuestion.setQuestionId(questionId);
        // 5.插入到试卷题目关系表
        examPaperQuestionMapper.insert(examPaperQuestion);


        ExamPaper examPaper = new ExamPaper();
        // 6.试卷id
        examPaper.setId(examPaperId);
        // 7.试卷总分的增值 - 添加题目的分数
        examPaper.setTotalScore((short) questionScore);
        // 8.根据题目id去题目表查出题目类型
        Question question = questionMapper.queryById(questionId);
        QuestionType questionType = question.getType();
        switch (questionType){
            case CHOICE:
                // 8.1 添加的题目的类型为选择题，试卷的选择题数量增值为1
                examPaper.setSelectQuestionNumber((short) 1);
                break;
            case GAP:
                // 8.2 添加的题目的类型为填空题，试卷的填空题数量增值为1
                examPaper.setBlackfillQuestionNumber((short) 1);
                break;
            case SUBJECTIVE:
                // 8.3 添加的题目的类型为主观题，试卷的主观题数量增值为1
                examPaper.setSubjectiveQuestionNumber((short) 1);
                break;
        }
        // 9.试卷更新时间
        examPaper.setUpdateTime(LocalDateTime.now());
        // 10.根据试卷id更新试卷表exam_paper
        examPaperMapper.updateById(examPaper);

    }


    /**
     * 根据试卷id和题目id删除试卷的题目
     * @param examPaperId
     * @param questionId
     */
    @Transactional
    @Override
    public void subQuestion(Long examPaperId, Long questionId) {

        // 1.判断当前教师是否有组卷权限
        this.isSetAuth();

        // 2.判断题目是否在试卷中
        ExamPaperQuestion questionTest = examPaperQuestionMapper.queryByExamPaperIdAndQuestionId(examPaperId, questionId);
        if (questionTest == null){
            // 题目不在试卷中，抛出删除题目异常
            throw new QuestionRepeatError(MessageConstant.EXAM_PAPER_QUESTION_DELETE_FAILED);
        }

        // 3.根据题目id和试卷id去试卷题目关系表查出题目分值   根据题目id去题目表查出题目类型
        ExamPaperQuestionVO examPaperQuestionVO =
                examPaperQuestionMapper.selectByExamPaperIdAndQuestionId(examPaperId, questionId);
        // 4.题目分值
        float questionScore = examPaperQuestionVO.getQuestionScore();
        // 5.题目类型
        QuestionType questionType = examPaperQuestionVO.getQuestionType();

        ExamPaper examPaper = new ExamPaper();
        // 6.试卷id
        examPaper.setId(examPaperId);

        // 7.删除的题目类型
        switch (questionType){
            case CHOICE:
                // 7.1 删除的题目的类型为选择题，试卷的选择题增值为-1
                examPaper.setSelectQuestionNumber((short) -1);
                break;
            case GAP:
                // 7.2 删除的题目的类型为填空题，试卷的填空题增值为-1
                examPaper.setBlackfillQuestionNumber((short) -1);
                break;
            case SUBJECTIVE:
                // 7.3 删除的题目的类型为主观题，试卷的主观题增值为-1
                examPaper.setSubjectiveQuestionNumber((short) -1);
                break;
        }
        // 8.试卷总分的增值
        examPaper.setTotalScore((short)-questionScore);

        // 9.更新时间
        examPaper.setUpdateTime(LocalDateTime.now());

        // 10.根据试卷id更新试卷表exam_paper
        examPaperMapper.updateById(examPaper);


        // 11.根据题目id和试卷id去试卷题目关系表删除
        examPaperQuestionMapper.deleteByExamPaperIdAndQuestionId(examPaperId, questionId);

    }


    /**
     * 判断当前教师是否有组卷权限
     */
    private void isSetAuth(){
        // 1.教师id
        Long teacherId = BaseContext.getCurrentId();

        // 2.获取当前教师的组卷权限
        Result<PermissionVO> permissionVOResult = permissionService.queryByTeacherId(teacherId);
        PermissionVO permissionVO = permissionVOResult.getData();
        Boolean setAuth = permissionVO.getSetAuth();

        // 3.判断当前教师是否有组卷权限
        if (!setAuth){
            // 当前教师没有组卷权限，抛出权限异常
            throw new PermissionErrorException(MessageConstant.PERMISSION_ERROR);
        }
    }

}
